<template>
  <el-dialog
    :title="!dataForm.rentbuyId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="详情信息" prop="rentbuyContent">
      <el-input v-model="dataForm.rentbuyContent" placeholder="详情信息"></el-input>
    </el-form-item>
    <el-form-item label="意向区域id" prop="rentbuyAid">
      <el-input v-model="dataForm.rentbuyAid" placeholder="意向区域id"></el-input>
    </el-form-item>
    <el-form-item label="意向价格" prop="rentbuyIntendedPrice">
      <el-input v-model="dataForm.rentbuyIntendedPrice" placeholder="意向价格"></el-input>
    </el-form-item>
    <el-form-item label="发布时间" prop="rentbuyCreatedate">
      <el-input v-model="dataForm.rentbuyCreatedate" placeholder="发布时间"></el-input>
    </el-form-item>
    <el-form-item label="状态:0租赁1购房" prop="rentbuyStatus">
      <el-input v-model="dataForm.rentbuyStatus" placeholder="状态:0租赁1购房"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          rentbuyId: 0,
          userId: '',
          rentbuyContent: '',
          rentbuyAid: '',
          rentbuyIntendedPrice: '',
          rentbuyCreatedate: '',
          rentbuyStatus: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          rentbuyContent: [
            { required: true, message: '详情信息不能为空', trigger: 'blur' }
          ],
          rentbuyAid: [
            { required: true, message: '意向区域id不能为空', trigger: 'blur' }
          ],
          rentbuyIntendedPrice: [
            { required: true, message: '意向价格不能为空', trigger: 'blur' }
          ],
          rentbuyCreatedate: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          rentbuyStatus: [
            { required: true, message: '状态:0租赁1购房不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.rentbuyId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rentbuyId) {
            this.$http({
              url: this.$http.adornUrl(`/consumer/rentbuy/info/${this.dataForm.rentbuyId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.rentbuy.userId
                this.dataForm.rentbuyContent = data.rentbuy.rentbuyContent
                this.dataForm.rentbuyAid = data.rentbuy.rentbuyAid
                this.dataForm.rentbuyIntendedPrice = data.rentbuy.rentbuyIntendedPrice
                this.dataForm.rentbuyCreatedate = data.rentbuy.rentbuyCreatedate
                this.dataForm.rentbuyStatus = data.rentbuy.rentbuyStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/consumer/rentbuy/${!this.dataForm.rentbuyId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rentbuyId': this.dataForm.rentbuyId || undefined,
                'userId': this.dataForm.userId,
                'rentbuyContent': this.dataForm.rentbuyContent,
                'rentbuyAid': this.dataForm.rentbuyAid,
                'rentbuyIntendedPrice': this.dataForm.rentbuyIntendedPrice,
                'rentbuyCreatedate': this.dataForm.rentbuyCreatedate,
                'rentbuyStatus': this.dataForm.rentbuyStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
